<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .input-box {
            margin: 10px;
            display: flex;
            align-items: center;
        }

        .input-box input {
            flex: 1;
            height: 40px;
            border: 1px solid #ccc;
            outline: none;
            padding-left: 20px;
            border-radius: 5px;
        }

        .input-box span {
            text-align: center;
            color: lightseagreen;
            width: 60px;
        }

        .list {
            margin: 10px;
        }

        .list .task {
            margin: 20px 0;
            display: flex;
            justify-content: space-between;
            padding: 15px;
            background-color: red;
            color: white;
        }

        .list .task.finished {
            background-color: lightseagreen;
        }

        .empty {
            text-align: center;
            margin: 50px 0;
            color: red;
        }

        .opts {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .opts span {
            padding: 10px 20px;
            background-color: lightseagreen;
            color: white;
        }

        .opts span.unfinished {
            background-color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="input-box">
            <input type="text" placeholder="输入任务名称" v-model="inputText">
            <span @click="add">添加</span>
        </div>
        <div class="opts">
            <span>全部任务( {{ tasklist.length }} )</span>
            <span>已完成( {{finishCount}} )</span>
            <span class="unfinished">未完成( {{ unfinishCount }} )</span>
        </div>
        <h4>任务列表</h4>
        <div class="list">
            <div :class="{task:true,finished:item.status}" v-for="(item,index) in tasklist" :key="index">
                <input type="checkbox" @click="changeStatus(item)">
                <div class="name"> {{item.name}} </div>
                <span class="btn" @click="deleteTask(item)">删除</span>
            </div>
            <div class="empty" v-show="tasklist.length == 0">任务列表空空如也!</div>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: "#app",
        data: {
            inputText: '',//保留输入框中的值
            tasklist: [],
            finishCount: 0,
            unfinishCount: 0
        },
        methods: {
            add() {//添加任务
                if (this.inputText) {//输入框不能为空，才能添加任务
                    //构造一个任务对象
                    var obj = {
                        id: this.tasklist.length + 1, name: this.inputText,//xhr.status:获取当前服务器的响应状态  200=>成功
                        status: false
                    };
                    //将任务对象，添加到任务列表中
                    this.tasklist.push(obj);
                    //清空输入框
                    this.inputText = '';
                }
            },
            changeStatus(item) {//更改任务状态，item参数就是当前点击的任务对象
                item.status = !item.status;
            },
            deleteTask(item) {
                var i = this.tasklist.findIndex((it) => {
                    return it.id == item.id
                })
                if (i != -1) {
                    this.tasklist.splice(i, 1)
                }
            }
        },
        computed: {
            /* finishCount(){
                return this.tasklist.filter((item)=>item.status).length
            },
            unfinishCount(){
                return this.tasklist.length - this.tasklist.filter((item)=>item.status).length
            } */
        },
        watch: {
            /*  //写法一：函数的形式
             tasklist(){
                 console.log('监听到tasklist发生了改变');
                 this.finishCount = this.tasklist.filter((item) => { return item.status }).length;
                 this.unfinishCount = this.tasklist.length - this.tasklist.filter((item) => { return item.status }).length;
             } */
            //写法二：对象的形式
            tasklist: {
                handler() { //这个位置的函数名必须是handler
                    console.log('监听到tasklist发生了改变');
                    this.finishCount = this.tasklist.filter((item) => { return item.status }).length;
                    this.unfinishCount = this.tasklist.length - this.tasklist.filter((item) => { return item.status }).length;
                },
                deep: true
            }
        }
    })
</script>